package com.dykj.fireFighting.server.base.emergency.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.base.emergency.dto.EmergencyPlanTwoDto;
import com.dykj.fireFighting.api.base.emergency.entity.EmergencyPlanTwo;
import com.dykj.fireFighting.api.base.emergency.service.IEmergencyPlanTwoService;
import com.dykj.fireFighting.api.base.emergency.vo.EmergencyPlanTwoVo;
import com.dykj.fireFighting.api.base.emergency.vo.EmergencyTypeMonthVo;
import com.dykj.fireFighting.api.base.emergency.vo.EmergencyTypeUnitVo;
import com.dykj.fireFighting.api.base.emergency.vo.EmergencyTypeVo;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.base.emergency.mapper.EmergencyPlanTwoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 应急预案新版
 *
 * @Author JacksonTu
 * @Date 2021-08-26 14:40:38
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class EmergencyPlanTwoServiceImpl extends BaseServiceImpl<EmergencyPlanTwoMapper, EmergencyPlanTwo> implements IEmergencyPlanTwoService {


    @Autowired
    private ISysDirectoryService sysDirectoryService;
    @Autowired
    private IEnterpriseService enterpriseService;


    @Override
    public List<EmergencyPlanTwo> rankScore(String start, String end, String type, String shudi, List<String> unitList) {
        return this.baseMapper.rankScore(start, end, type, shudi, unitList);
    }

    @Override
    public List<EmergencyTypeUnitVo> typeUnit(String start, String end, String type, String shudi, List<String> unitList) {
        return this.baseMapper.typeUnit(start, end, type, shudi, unitList);
    }

    @Override
    public List<EmergencyTypeVo> type(String start, String end, String shudi, List<String> unitList) {
        //获取所有的预案类型
        List<SysDirectory> list = sysDirectoryService.queryDataByType("emergency");
        List<EmergencyTypeVo> retrunList = new ArrayList<>();
        for (SysDirectory s : list) {
            EmergencyTypeVo vo = new EmergencyTypeVo();
            vo.setId(s.getId().toString());
            vo.setName(s.getValue());
            //拿着id找数字
            List<EmergencyTypeMonthVo> listMonth = this.baseMapper.typeMonth(start, end, s.getId().toString(), shudi, unitList);
            listMonth.addAll(addZero(listMonth));

            vo.setList(listMonth);
            retrunList.add(vo);

        }

        return retrunList;
    }

    @Override
    public PageVo<EmergencyPlanTwoVo> queryAllList(EmergencyPlanTwoDto emergencyPlanTwoDto) {
        Map<String, Object> params = new HashMap<>();

        if (StringUtils.isNotBlank(emergencyPlanTwoDto.getUnitId())) {
            params.put("unitId", emergencyPlanTwoDto.getUnitId());
        } else {
            List<Enterprise> enterpriseList = enterpriseService.queryUnitSelect(emergencyPlanTwoDto.getJkzxUnitId());
            if (CollectionUtil.isNotEmpty(enterpriseList)) {
                params.put("enterpriseList", enterpriseList);
            }
        }
        params.put("title","%" + emergencyPlanTwoDto.getTitle() + "%");
        params.put("status",emergencyPlanTwoDto.getStatus());
        params.put("type",emergencyPlanTwoDto.getType());
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(emergencyPlanTwoDto.getPage());
        // 设置页大小
        page.setSize(emergencyPlanTwoDto.getLimit());
        return new PageVo<EmergencyPlanTwoVo>(this.baseMapper.queryList(page, params));
    }

    @Override
    public EmergencyPlanTwo getInfoById(String id) {
        return this.baseMapper.getInfoById(id);
    }

    @Override
    public EmergencyPlanTwo SelectInfoByBuildingId(String id) {
        return baseMapper.SelectInfoByBuildingId(id);
    }

    @Override
    public PageVo<EmergencyPlanTwoVo> queryList(EmergencyPlanTwoDto emergencyPlanTwoDto) {
        Map<String, Object> params = new HashMap<>();

        params.put("unitId", emergencyPlanTwoDto.getUnitId());
        if (StringUtils.isNotBlank(emergencyPlanTwoDto.getTitle())) {
            params.put("title", "%" + emergencyPlanTwoDto.getTitle() + "%");
        }
        params.put("status",emergencyPlanTwoDto.getStatus());
        params.put("type",emergencyPlanTwoDto.getType());
        Page page = new Page();
        // 设置当前页码
        page.setCurrent(emergencyPlanTwoDto.getPage());
        // 设置页大小
        page.setSize(emergencyPlanTwoDto.getLimit());
        return new PageVo<EmergencyPlanTwoVo>(this.baseMapper.queryList(page, params));
    }

    public List<EmergencyTypeMonthVo> addZero(List<EmergencyTypeMonthVo> list) {
        List<EmergencyTypeMonthVo> reList = new ArrayList<>();
        if (list.size() == 0) {
            for (int i = 0; i < 12; i++) {
                EmergencyTypeMonthVo vo = new EmergencyTypeMonthVo();
                vo.setNum(0);
                vo.setMonth(i + 1);
                reList.add(vo);
            }

        } else {
            //求差值
            List<Integer> listT = new ArrayList<>();
            for (int i = 0; i < 12; i++) {
                listT.add(i + 1);
            }
            List<Integer> dataList = new ArrayList<>();
            for (EmergencyTypeMonthVo vo : list) {
                dataList.add(vo.getMonth());
            }
            List<Integer> reIntegerList = listT.stream().filter(item -> !dataList.contains(item)).collect(Collectors.toList());
            for (Integer i : reIntegerList) {
                EmergencyTypeMonthVo vo = new EmergencyTypeMonthVo();
                vo.setNum(0);
                vo.setMonth(i);
                reList.add(vo);
            }

        }
        return reList;
    }
}
